﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rel.Expressions
{
    
	/// <summary>
	/// Performs an OR (|) subexpression on two independent regular expression chains.
	/// </summary>
    public class Or:RegBase
    {
        public Or(RelGroup first, RelGroup second,  RelGroup parent)
            :base(parent)
        {
            AddChildExpression(first);
            AddChildExpression(second); 
        }

        public Or(List<RelGroup> conditions, RelGroup parent)
            :base(parent)
        {
            conditions.ForEach(o => 
                {
                AddChildExpression(o);
                }
                );
        }

        public override  string Pattern
        {
            get
            {

                if (ChildExpressions.Count > 0)
                {
                    string pattern =
                        (UseExtract ? "(?<" + GroupName + ">(" : "(");

                    pattern += "(" + ChildExpressions.First().Pattern + ")";

                    for (int x = 1; x < ChildExpressions.Count; x++)
                    {
                        pattern += "|(" + ChildExpressions[x].Pattern + ")";
                    }

                    pattern += UseExtract ? "))" : ")";

                    return pattern;

                }
                else
                {
                    return "";
                }
            }
        }


    }
}
